package com.leha.mp.api.controller;

import com.leha.mp.api.form.MsgForm;
import com.leha.mp.api.websockets.WSHandler;
import com.leha.mp.core.Utils.Page;
import com.leha.mp.core.services.ConfluenceServices;
import com.leha.mp.core.services.OrderServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.xml.sax.InputSource;
import utils.APIResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;

@RestController
@Api(value = "/biz", description = "业务类Api")
@RequestMapping("/biz")
@ResponseStatus(HttpStatus.OK)
public class BizController {
    @Autowired
    ConfluenceServices confluenceServices;
    @Autowired
    OrderServices orderServices;

    private static Logger logger = Logger.getLogger(BizController.class);
    @PostMapping("endcall")
    @ApiOperation("结束通话回调接口")
    public void notifyEndCall(HttpServletRequest request, HttpServletResponse response){
        try{
            BufferedReader reader = request.getReader();
            String line = "";
            StringBuffer inputString = new StringBuffer();
            try{
                while ((line = reader.readLine()) != null) {
                    inputString.append(line);
                }
                if(reader!=null){
                    reader.close();
                }
                logger.info("----[融联回调]接收到的报文---"+inputString.toString());
                StringReader read = new StringReader(inputString.toString());
                // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
                InputSource source = new InputSource(read);
                // 创建一个新的SAXBuilder
                SAXBuilder sb = new SAXBuilder();
                // 通过输入源构造一个Document
                Document doc;
                doc = (Document) sb.build(source);

                Element root = doc.getRootElement();// 指向根节点
                List<Element> list = root.getChildren();
                HashMap<String, String> ret = new HashMap<>();
                if(list!=null&&list.size()>0){
                    for (Element element : list) {
                        ret.put(element.getName(), element.getValue());
                    }
                }
                confluenceServices.endPhone(ret);
            }catch(Exception e){
                e.printStackTrace();
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    @PostMapping("sendMsg")
    @ApiOperation("图文咨询发送消息_医生端，用户端")
    public APIResult saveMsg(MsgForm msgForm) {
        return new APIResult(confluenceServices.saveMsg(msgForm.toDateModel()));
    }

    @GetMapping("/{consultingId}")
    @ApiOperation("获取图文咨询消息列表_用户端，医生端")
    public APIResult getConsultingMsg(@PathVariable Integer consultingId, Page page) {
        return new APIResult(confluenceServices.getConsultingMsg(consultingId, page));
    }

    @PostMapping("recall")
    @ApiOperation("重新呼叫_医生端，用户端")
    public APIResult recall(Integer consultingId) {
        return new APIResult(confluenceServices.recallPhone(consultingId));
    }

    @PostMapping("cancelConsulting")
    @ApiOperation("取消咨询_后台接口")
    public APIResult cancelConsulting(Integer consultId) {
        return new APIResult(orderServices.cancelConsulting(consultId));
    }

    @GetMapping("/detail/{consultingId}")
    @ApiOperation("获取咨询订单详情")
    public APIResult getConsulting(@PathVariable Integer consultingId) {
        return new APIResult(confluenceServices.getConsultingbyId(consultingId));
    }
}
